package com.wjf.ks_test.vo;

/**
 * @author 吴建飞
 * @version 1.0
 * @since 2024/11/11
 */
public class Triangle implements Polygon{
    private Vertex[] vertices;

    public Triangle(Vertex[] vertices) {
        this.vertices = vertices;
    }

    /**
     * 简单判断，三角形需要三个顶点
     * @param vertices
     * @param otherParams
     * @return 布尔类型
     */
    @Override
    public boolean canForm(Vertex[] vertices, double[] otherParams) {
        return vertices.length == 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }

    /**
     *  这里使用海伦公式计算三角形面积，示例代码简化处理
     * @return 面积
     */
    @Override
    public double calcArea() {
        double a = vertices[0].calcDistance(vertices[1]);
        double b = vertices[1].calcDistance(vertices[2]);
        double c = vertices[2].calcDistance(vertices[0]);
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }
}
